﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TMaterialGroupRepository : BaseRepository<TMaterialGroup>, ITMaterialGroupRepository
    {
        public TMaterialGroupRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddTMaterialGroup(TMaterialGroup m)
        {
            var mobj = Context.TMaterialGroups.AsNoTracking().FirstOrDefault(x => x.Code == m.Code);
            if(mobj!=null)
            {
                return false;
            }
            m.CreateUserId = m.NowUserId;
            m.CreateUserName = m.NowUserRealName;
            m.CreateDate = DateTime.Now;
            m.ModifyDate = DateTime.Now;
            m.ModifyUserName = m.NowUserRealName;
            m.ModifyUserId = m.NowUserId;

            Context.TMaterialGroups.Add(m);
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelTMaterialGroup(int mid)
        {
            var mobj = Context.TMaterialGroups.Find(mid);
            if(mobj!=null)
            {
                Context.TMaterialGroups.Remove(mobj);
            }
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TMaterialGroup GetTMaterialGroupById(int mid)
        {
            return Context.TMaterialGroups.Find(mid);
        }

        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="manme"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TMaterialGroup> GetTMaterialGroupList(string manme, int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TMaterialGroups.AsQueryable();
            if(!string.IsNullOrEmpty(manme))
            {
                list=list.Where(x=>x.Name.Contains(manme));
            }

            totalCount = list.Count();

            return list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdTMaterialGroup(TMaterialGroup m)
        {
            var list = Context.TMaterialGroups.AsNoTracking().FirstOrDefault(x => x.Id != m.Id && x.Code == m.Code);

            if (list != null)
            {
                return false;
            }
            m.ModifyDate = DateTime.Now;
            m.ModifyUserId = m.NowUserId;
            m.ModifyUserName = m.NowUserRealName;
            Context.TMaterialGroups.Update(m);
            return Context.SaveChanges() > 0;
        }
    }
}
